package Purl::Config;
use File::Basename;
use strict;

require Exporter;
our @ISA    = qw(Exporter);
our @EXPORT = qw(config);

use Config::General;
use Cwd;

my %_config;

sub init {
    my %args = @_;
    my @path;
    my $defaultDir = getcwd;
    $defaultDir =~ s/(.*)\/bin/\1/;
    my ( $confFile, $confPath ) = fileparse( $args{"configFile"} );
    if ( $args{"configFile"} =~ /^\// ) {

    }
    elsif ( $args{"configFile"} =~ /\// ) {
        $confPath = $defaultDir . "/" . $confPath;
    }
    else {
        $confPath = $defaultDir;
    }
    @path = ($confPath);

    #   my @path = ( "$confPath", "$defaultDir/conf" );
    print STDERR "INSTALLDIR: " . $defaultDir . "\n";
    print STDERR "ConfigFile: " . $args{"configFile"} . "\n";

    my $conf = new Config::General(
        -ConfigFile     => $confFile,
        -ConfigPath     => \@path,
        -LowerCaseNames => 1,
        -ExtendedAccess => 1,
        -AutoTrue       => 1
    );
    %_config = $conf->getall;
    $_config{"mushdir"} = $defaultDir if ( !$_config{"mushdir"} );

    #    foreach my $c ( keys %_config ) {
    #        print STDERR "$c - $_config{$c}\n";
    #    }
}

sub _getConfig {
    my $args = shift;
    my $cat;
    my $con;
    my $return = "";

    if ( $args =~ /:/ ) {
        ( $cat, $con ) = split( /:/, $args, 2 );
        $return = $_config{$cat}->{$con};
    }
    else {
        $con    = $args;
        $return = $_config{$con};
    }

    return -1 if ( $return eq "" );
    return $return;
}

sub _listConfig {
    my $args = shift;
    my $item;
    my $return = "";

    foreach $item ( sort keys %_config ) {
        my $itemref = $_config{$item};
        if ( ref($itemref) eq "HASH" ) {
            my $subitem;
            $return .= "$item\n";
            foreach $subitem ( sort keys %$itemref ) {
                $return .= "\t$subitem : $itemref->{$subitem}\n";
            }
        }
        else {
            $return .= "$item : $_config{$item}\n";
        }
    }

    return -1 if ( $return eq "" );
    return $return;
}

sub _setConfig {
    my %args = @_;
    my $cat;
    my $con;
    my $return = "";

    if ( $args{target} =~ /:/ ) {
        ( $cat, $con ) = split( /:/, $args{target}, 2 );
        if ( $args{value} eq "" ) {
            undef $_config{$cat}->{$con};
        }
        else {
            $_config{$cat}->{$con} = $args{value};

        }
        $return = "$args{target} - Set\n";
    }
    else {
        $cat = $args{target};
        if ( $args{value} eq "" ) {
            undef $_config{$cat};
        }
        else {
            $_config{$cat} = $args{value};

        }
        $return = "$args{target} - Set\n";
    }

    return -1 if ( $return eq "" );
    return $return;
}

sub config {

    #    print STDERR "Config Called!\n";
    if ( scalar @_ == 0 ) {
        return "No Config Item Supplied";
    }
    elsif ( scalar @_ == 1 ) {
        my $args = shift;

        return _getConfig($args);
    }
    else {
        my %args = @_;
        if ( $args{action} eq "get" ) {
            return _getConfig( $args{target} );
        }
        elsif ( $args{action} eq "set" ) {
            return _setConfig(%args);
        }    
        elsif ( $args{action} eq "list" ) {
            return _listConfig();
        }
        else {
            print STDERR "Unknown Action: $args{action}\n;";
            return -1;
        }
    }

}
1;
